/****************************************************************************
 * Copyright (C) 2011
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * CEmuList.cpp
 *
 * for SaveGame Manager GX 2011
 ***************************************************************************/
#include <algorithm>

#include "../../Saves/SaveTools.h"
#include "../../FileOperations/DirList.h"
#include "../../FileOperations/fileops.h"
#include "esavebrowser.h"

ESaveBrowser::ESaveBrowser()
	:SaveBrowser()
{
	SearchChars.clear();
	
	//!Reset and prepare browser
	ResetBrowser();
	
	curENandPath = "";
}

static bool NameSortCallback(const SaveStruct *a, const SaveStruct *b)
{
	return (stricmp((wstr2str(((SaveStruct *)a)->name)).c_str(), (wstr2str(((SaveStruct *)b)->name)).c_str()) < 0);
}

void ESaveBrowser::GetList(std::string emuPath)
{
	ResetBrowser();
	
	std::string titlePath = emuPath;
	titlePath += "/title";
	
	DirList * dirList = new DirList(titlePath, NULL, DirList::Dirs);
	for(int i = 0; i < dirList->GetFilecount(); i++)
	{
		std::string dirName = dirList->GetFilename(i);
		if(!dirName.compare("00000001") || !dirName.compare("00010002") || !dirName.compare("00010005") || !dirName.compare("00010008"))
			continue;
		
		DirList * emuList = new DirList(dirList->GetFilepath(i), NULL, DirList::Dirs);
		for(int j = 0; j < emuList->GetFilecount(); j++)
		{
			std::string savepath = emuList->GetFilepath(j);
			savepath += "/data";
			if(!SaveTools::CheckSave(savepath))
				continue;
			
			AddEntry();
			int pos = browserList.size()-1;
			
			//! Set title name & subname
			browserList.at(pos)->name = SaveTools::GetDeviceName(savepath);
			browserList.at(pos)->subname = SaveTools::GetDeviceSubname(savepath);
			
			//! Set titleid
			std::string str_tid = dirName;
			str_tid += emuList->GetFilename(j);
			u64 tid = SaveTools::StrToHex64(str_tid);
			browserList.at(pos)->tid = tid;
			
			//! Set title ID[4]
			browserList.at(pos)->ID = SaveTools::titleText((u32)(tid >> 32),(u32)(tid & 0xFFFFFFFF));
			
			//! Set title blockSize
			u64 foldersize = 0;
			u32 filenumber = 0;
			GetFolderSize(savepath, &foldersize, &filenumber);
			browserList.at(pos)->blocksize = ((float)(foldersize/(float)NAND_BLOCK_SIZE));
			
			//! Check if the save is already installed in real nand
			browserList.at(pos)->isNotInstalled = !SaveTools::CheckIfInstalled(tid);
		}
		delete emuList;
	}
	delete dirList;
	
	//! set GameTDB Titles
	LoadTitlesFromGameTDB();
	
	if(!browserList.empty())
		curENandPath = emuPath;
	
	if(browserList.size() > 1)
		std::sort(browserList.begin(), browserList.end(), NameSortCallback);
}
